/*!
 @header SellerServiceOpeningViewController.m
 @author 周益
 @version 1.0.0 2013/03/22 Creation
 Copyright (c) 2013年 mypocketbaby. All rights reserved.
 */

#import "SellerServiceOpeningViewController.h"
#import "config.h"
#import "Helper.h"

//  -业务逻辑细节说明-
//
//  @abstract       SellerServiceOpeningViewController功能较为简单，不再赘述。在点击开通按钮后，会根据当前用户的卖家服务申请类型申请不同的业务类型：开通普通卖家、开通担保卖家和升级为担保卖家

//  @discussion

//  @notes


//  -类修改日志-
//  修改人   修改时间   针对版本   修改描述

@interface SellerServiceOpeningViewController ()

@end

@implementation SellerServiceOpeningViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

/*!
 @abstract 自定义的SellerServiceOpeningViewController初始化方法
 @param type 服务申请类型
 @result SellerServiceOpeningViewController的实例
 */
- (id)initWithType:(ServiceApplyType)type
{
    self = [super init];
    if (self) {
        _type = type;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    self.view.backgroundColor = _SYS_COLOR_BACKGROUND;
    
    UIButton *backButton = [Helper buttonWithStyle:ButtonStyleNavigationReturn];
    [backButton addTarget:self action:@selector(backButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    
    
    //申请卖家服务的条件view
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 416.0f)];
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10.0f, _SYS_SCREEN_HEIGHT - 20.0f - 44.0f - 44.0f - 15.0f , 300.0f, 44.0f)];
    [button setBackgroundImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"com_btn_006_n@2x" ofType:@"png"]] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.titleLabel.font = _SYS_FONT_007;
    
    //如果是开通普通卖家服务
    if (_type == ServiceApplyNormalType) {
        
        self.title = _L(@"title_32");
        
        imageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"more_img_001@2x" ofType:@"png"]];
        [button setTitle:_L(@"title_32") forState:UIControlStateNormal];   //申请开通普通卖家
    }
    //如果是开通担保卖家服务
    else if (_type == ServiceApplyVIPType){
        
        self.title = _L(@"title_33");
        
        imageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"more_img_002@2x" ofType:@"png"]];
        [button setTitle:_L(@"title_33") forState:UIControlStateNormal];   //申请开通担保卖家
    }
    //如果是升级担保卖家服务
    else {
    
        self.title = _L(@"title_34");
        
        imageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"more_img_003@2x" ofType:@"png"]];
        [button setTitle:_L(@"title_34") forState:UIControlStateNormal];   //升级为担保卖家
    }

    [self.view addSubview:imageView];
    [self.view addSubview:button];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 自定义事件

/*!
 @abstract 返回按钮事件
 */
- (void)backButtonEvent:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

/*!
 @abstract 申请按钮事件
 */
- (void)buttonEvent:(id)sender
{
    //如果是开通普通卖家服务
    if (_type == ServiceApplyNormalType) {
        
        [self serviceApplyRequestWithType:ServiceApplyNormalType];
        
    //如果是开通担保卖家服务,分直接升级问担保卖家和从普通卖家升级为担保卖家
    }else if(_type == ServiceApplyVIPType){
        
        [self serviceApplyRequestWithType:ServiceApplyVIPType];
        
    }else{
        [self serviceApplyRequestWithType:ServiceUpgradeToVIPType];
    }
}

#pragma mark - 私有方法

/*!
 @abstract 根据服务申请类型，向服务器请求开通卖家服务
 @param type 服务申请类型
 */
- (void)serviceApplyRequestWithType:(ServiceApplyType)type
{
    JSONRequest *request = [[JSONRequest alloc] initWithURLString:_JSON_URL_ACCOUNT_SELLERSERVICE_APPLY];
    request.delegate = self;
    request.hudOn = YES;
    request.hudTitle = _L(@"title_loading_13");
    [request setValue:[NSString stringWithFormat:@"%d", type] forParameter:@"type"];
    [request asynchronous];
}

#pragma mark - JSONRequestDelegate

- (void)requestDidLoadJSON:(NSDictionary *)dict
{
    JMFBase* jmf = [[JMFBase alloc] initWithJSONDictionary:dict];
    
    if (jmf.status == JMStatusSucceed) {
        
        [Helper showAlertWithTitle:nil andMessage:jmf.message];
    }
    else {
        [Helper easyShowJMFStatus:jmf];
    }
}

//
- (void)requestFailWithNetworkUnavailabe
{
    [Helper showAlertWithTitle:nil andMessage:_L(@"title_networkunavailabe")];
}
@end
